package com.lanmao.blog.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.lanmao.blog.vo.BlogTags;
import com.lanmao.blog.entity.Blog;
import com.lanmao.blog.entity.Count;
import com.lanmao.blog.service.BlogService;
import com.lanmao.blog.utils.CheckForm;
import com.lanmao.blog.utils.R;
import com.lanmao.blog.vo.FristBlog;
import com.lanmao.blog.vo.HotBlog;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lanmao
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    BlogService blogService;

    /**
     * 分页查询
     * @return
     */

    @GetMapping("/getByPage")
    @ApiOperation("博客分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "每页的数量"),
            @ApiImplicitParam(name = "published",value = "是否公开"),
            @ApiImplicitParam(name = "flag",value = "原创或转载"),
            @ApiImplicitParam(name = "share_statement",value = "草稿"),
            @ApiImplicitParam(name = "is_delete",value = "是否已删除"),
    })
    public R getByPage(Long current, Long size, Boolean published,
                       String flag, Boolean share_statement, Boolean is_delete){
        Page<Blog> blogs = blogService.pageBlogs(current, size, published, flag, share_statement, is_delete);

        return R.ok().data("data",blogs);
    }
    /**
     * 根据博客标题查询
     * @param title
     * @return
     */
    @GetMapping("/getByTitle")
    @ApiOperation("通过文章标题查询")
    @ApiImplicitParam(name = "title",value = "文章的标题")
    public R findByTitle(String title){
        List<Blog> list = blogService.getByTitle(title);
        return R.ok().data("data",list);
    }
    /**
     * 根据博客标题查询
     * @param title
     * @return
     */
    @GetMapping("/vuegetByTitle")
    @ApiOperation("通过文章标题查询")
    @ApiImplicitParam(name = "title",value = "文章的标题")
    public R vuegetByTitle(String title){
        List<FristBlog> list = blogService.vuegetByTitle(title);
        return R.ok().data("data",list);
    }


    /**
     * 保存文章和标签
     * @param blogTags
     * @return
     */
    @PostMapping("/savaBT")
    public R saveBlogTag(@RequestBody BlogTags blogTags) {
        R check = CheckForm.check(blogTags);
        if (check.getCode() == -1) {
            return check;
        }else {
            boolean flag= blogService.saveBT(blogTags);
            if (flag){
                return R.ok().message("添加成功");
            }else {
                return R.error().message("添加失败");
            }

        }

    }

    /**
     * 暂时保存博客
     * @param blogTags
     * @return
     */
    @PostMapping("/temporarySave")
    public R temporarySave(@RequestBody BlogTags blogTags){
        R checkTemporaryBlog = CheckForm.checkTemporaryBlog(blogTags);
        if (checkTemporaryBlog.getCode() == -1) {
            return checkTemporaryBlog;
        }
        else {
            boolean flag=blogService.temporarySave(blogTags);
            if (flag){
                return R.ok().message("添加成功");
            }
            return R.error().message("添加失败");
        }
    }

    /**
     * 更新博客和标签
     * @param blogTags
     * @return
     */
    @PutMapping("/updateBlog")
    public R updateBlog(@RequestBody BlogTags blogTags){
        R checkUpdateBlog = CheckForm.check(blogTags);
        if (checkUpdateBlog.getCode() == -1) {
            return checkUpdateBlog;
        }
        else {
           boolean flag= blogService.updateBlog(blogTags);
           if (flag){
               return R.ok().message("更新博客成功");
           }
            return R.error().message("更新博客失败");
        }
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping("/logicDeleteBlog")
    public R logicDeleteBlog(String id){
        boolean flag= blogService.logicDeleteBlog(id);
        if (flag){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }

}

    /**
     * 删除博客和对应标签
     * @param id
     * @return
     */
    @DeleteMapping("/deleteBlog")
    public R deleteBlog(String id){
        Blog blog = blogService.getById(id);
        if (blog != null){
            boolean flag=blogService.deleteBT(id);
            if (flag){
                return R.ok().message("删除博客和标签成功");
            }else {
                return R.error().message("删除博客和标签失败");
            }
        }else {
            return R.error().message("没有数据");
        }
    }

    /**
     * 还原删除的博客
     * @param id
     * @return
     */
    @GetMapping("/recoveryBlog")
    public R recoveryBlog(String id){
        boolean flag= blogService.recoveryBlog(id);
        if (flag){
            return R.ok().message("还原博客成功");
        }
        return R.error().message("还原博客失败");
    }

    /**
     * 根据id查找博客
     * @param id
     * @return
     */
    @GetMapping("/getByBlogId")
    public R getByBlogId(String id){
        Blog blog = blogService.getByBlogId(id);
        return R.ok().data("data",blog);
    }

    /**
     * 统计数据
     * @return
     */
    @GetMapping("/countBlog")
    public R countBlog(){
        Count count=blogService.countBlog();
        return R.ok().data("data",count);
    }

    /**
     * 前台分页
     * @param current
     * @param size
     * @param type_id
     * @param tags_id
     * @return
     */
    @GetMapping("/vuefindByPage")
    public R prefindByPage(Long current, Long size, Long type_id, Long tags_id){
        HashMap<String,Object>  map=blogService.prefindByPage(current, size,type_id,tags_id);
        if (map.get("blogList")==null){
            return R.ok().message("没有数据");
        }else {
            return R.ok().data("data",map);
        }
    }
    @GetMapping("/vuefindByTag")
    public R vuefindByTag(Long current, Long size, Long type_id, Long tags_id){
        HashMap<String,Object>  map=blogService.vuefindByTag(current, size,type_id,tags_id);
        if (map.get("blogList")==null){
            return R.ok().message("没有数据");
        }else {
            return R.ok().data("data",map);
        }
    }

    @GetMapping("/vuefindHotBlog")
    public R prefindHotBlog(){
        List<HotBlog> list= blogService.prefindHotBlog();
        return R.ok().data("data",list);
    }

}

